package com.wwtech.epp.ui.activity.work.presenter;

import android.support.annotation.NonNull;

import com.socks.library.KLog;
import com.wwtech.epp.application.AppConfig;
import com.wwtech.epp.data.api.HttpAPIWrapper;
import com.wwtech.epp.entity.ApproveState;
import com.wwtech.epp.entity.BaseBack;
import com.wwtech.epp.ui.activity.work.ApproveDetailActivity;
import com.wwtech.epp.ui.activity.work.NewProjectActivity;
import com.wwtech.epp.ui.activity.work.contract.ApproveDetailContract;
import com.wwtech.epp.utils.ToastUtil;

import java.util.Map;

import javax.inject.Inject;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.work
 * @Description: presenter of ApproveDetailActivity
 * @date 2017/09/22 09:41:14
 */
public class ApproveDetailPresenter implements ApproveDetailContract.ApproveDetailContractPresenter{

    HttpAPIWrapper httpAPIWrapper;
    private final ApproveDetailContract.View mView;
    private CompositeDisposable mCompositeDisposable;
    private ApproveDetailActivity mActivity;

    @Inject
    public ApproveDetailPresenter(@NonNull HttpAPIWrapper httpAPIWrapper, @NonNull ApproveDetailContract.View view, ApproveDetailActivity activity) {
        mView = view;
        this.httpAPIWrapper = httpAPIWrapper;
        mCompositeDisposable = new CompositeDisposable();
        this.mActivity = activity;
    }
    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        if (!mCompositeDisposable.isDisposed()) {
             mCompositeDisposable.dispose();
        }
    }

    @Override
    public void getUIDesc(String dataId, Map map) {
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.getUIDesc(dataId, map)
                .subscribe(new Consumer<BaseBack>() {
                    @Override
                    public void accept(BaseBack baseBack) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.closeProgressDialog();
                        ToastUtil.displayShortToast(baseBack.getResultMsg());
                        if (baseBack.getResultCode() == 0) {
                            AppConfig.getInstance().mAppActivityManager.finishActivity(NewProjectActivity.class);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void removeApprove(Map map) {
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.removeApprove(map)
                .subscribe(new Consumer<BaseBack>() {
                    @Override
                    public void accept(BaseBack baseBack) throws Exception {
                        //isSuccesse
                        mView.closeProgressDialog();
                        mView.onRemoveApproveSuccess();
                        KLog.i("onSuccesse");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void operateApprove(Map map) {
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.operateApprove(map)
                .subscribe(new Consumer<BaseBack>() {
                    @Override
                    public void accept(BaseBack baseBack) throws Exception {
                        //isSuccesse
                        mView.closeProgressDialog();
                        mView.onOperateSuccess();
                        KLog.i("onSuccesse");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    /**
     * 获取该审批的通过详情
     * @param requestId
     * @param map
     */
    @Override
    public void getApproveDetail(String requestId, Map map) {
        Disposable disposable = httpAPIWrapper.getApproveInfo(requestId, map)
                .subscribe(new Consumer<ApproveState>() {
                    @Override
                    public void accept(ApproveState approveState) throws Exception {
                        //isSuccesse
                        mView.closeProgressDialog();
                        mView.onGetApproveStateSuccess(approveState);
                        KLog.i("onSuccesse");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }
}